﻿namespace DemoTest
{
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfDemoControlLibrary;

    /// <summary>
    /// Test for the PersonUserControl
    /// </summary>
    [TestClass]
    public class PersonUserControlTest
    {
        /// <summary>
        /// the local TestContext
        /// </summary>
        private TestContext testContextInstance;

        /// <summary>
        /// Control to test
        /// </summary>
        private CustomControl control;

        /// <summary>
        /// ViewModel for the Control
        /// </summary>
        private PersonUserControlViewModel viewModel;

        /// <summary>
        /// Gets or sets TestContext.
        /// </summary>
        public TestContext TestContext
        {
            get
            {
                return this.testContextInstance;
            }

            set
            {
                this.testContextInstance = value;
            }
        }

        /// <summary>
        /// Initialize method
        /// </summary>
        [TestInitialize]
        public void MyTestInitialize()
        {
            this.viewModel = new PersonUserControlViewModel();
            this.control = CustomControl.Start<PersonUserControl>(c => c.DataContext = this.viewModel);
        }

        /// <summary>
        /// Cleanup method
        /// </summary>
        [TestCleanup]
        public void MyTestCleanup()
        {
            if (this.control != null)
            {
                this.control.Stop();
            }
        }

        /// <summary>
        /// Test adding a person
        /// </summary>
        [TestMethod]
        public void TestAddPerson()
        {
            this.control.Get<TextBox>("txtFirstname").Value = "Roger";
            this.control.Get<TextBox>("txtName").Value = "Smith";
            this.control.Get<ComboBox>("cmbNationality").GetItem("Switzerland").Select();
            this.control.Get<Button>("btnAdd").Invoke();
            DataGrid_WpfToolkit dataGrid = this.control.Get<DataGrid_WpfToolkit>("dataGrid");

            Assert.AreEqual("Smith", dataGrid[0, 0].TextValue);
            Assert.AreEqual("Roger", dataGrid[0, 1].TextValue);
            Assert.AreEqual("Switzerland", dataGrid[0, 2].TextValue);
        }

        /// <summary>
        /// Test binding GUI to ViewModel
        /// </summary>
        [TestMethod]
        public void TestDataBindingGui2Vm()
        {
            this.control.Get<TextBox>("txtFirstname").Value = "Roger";
            this.control.Get<TextBox>("txtName").Value = "Smith";
            this.control.Get<ComboBox>("cmbNationality").GetItem("Switzerland").Select();

            Assert.AreEqual("Roger", this.viewModel.Firstname);
            Assert.AreEqual("Smith", this.viewModel.Name);
            Assert.AreEqual("Switzerland", this.viewModel.Nationality);
        }

        /// <summary>
        /// Test binding ViewModel to GUI
        /// </summary>
        [TestMethod]
        public void TestDataBindingVm2Gui()
        {
            this.viewModel.Firstname = "Roger";
            this.viewModel.Name = "Smith";
            this.viewModel.Nationality = "Switzerland";

            Assert.AreEqual("Roger", this.control.Get<TextBox>("txtFirstname").Value);
            Assert.AreEqual("Smith", this.control.Get<TextBox>("txtName").Value);
            Assert.AreEqual("Switzerland", this.control.Get<ComboBox>("cmbNationality").SelectedItem.TextValue);
        }
    }
}
